<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <template #title>
        <van-button
          class="search-btn"
          type="info"
          size="small"
          round
          icon="search"
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 标签栏 -->
    <!--通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签通过animated属性可以开启切换标签内容时的转场动通过swipeable属性可以开启滑动切换标签页-->
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      swipe-threshold="4"
    >
      <van-tab v-for="obj in channels" :key="obj.id" :title="obj.name">
        <!-- 文章列表组件 -->
        <article-list :channel="obj"></article-list>
      </van-tab>
      <!-- 汉堡按钮占位div -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <div class="border-left" style="width: 1px"></div>
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <!-- close-icon="close"//自定义关闭图标 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon-position="top-right"
      position="right"
      :style="{ width: '100%', height: '100%' }"
    >
      <channel-edit
        :myChannels="channels"
        v-model="active"
        @cutFn="cutFn"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
// 引入获取用户频道列表接口
import { getUserChannels } from "@/api/user.js";
// 引入文章列表组件
import ArticleList from "./components/ArticleList.vue";
//引入展示编辑频道组件
import ChannelEdit from "./components/ChannelEdit.vue";
//使用user判断他用户是否登录
import { mapState } from "vuex";
// 调用getItem中的TOUTIAO_CHANNELS未登录的时候本地有数据渲染频道列表
import { getItem } from "@/utils/storage.js";

export default {
  name: "HomePage",
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      active: 0, //当前所处频道
      channels: [], //存放获取的频道列表
      isChannelEditShow: false, //控制频道编辑弹出层的展示
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    //调用请求频道列表方法
    this.loadChannels();
  },
  mounted() {},
  methods: {
    //请求频道列表方法
    async loadChannels() {
      try {
        //判断用户是否登录 以及判断本地是否有数据
        const localChannels = getItem("TOUTIAO_CHANNELS");
        if (this.user || !localChannels) {
          //如果登录或者本地没有数据 调接口
          const { data } = await getUserChannels();
          this.channels = data.data.channels;
        } else {
          //未登录直接在本地拿
          this.channels = localChannels;
        }
      } catch (err) {
        this.$toast("请求频道列表失败请稍后重试");
      }
    },
    //在编辑层点击后切换频道方法
    cutFn() {
      this.isChannelEditShow = false;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
    .search-btn {
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;
      .van-icon {
        font-size: 32px;
      }
    }
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      width: 100%;
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      // 组件内部的属性
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }
    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      .border-left {
        height: 100px;
        position: absolute;
        left: 0;
        background-image: linear-gradient(
          transparent,
          #dfdfdf 25%,
          #999 50%,
          #dfdfdf 75%,
          transparent 100%
        );
      }
      // &:before {
      //   content: "";
      //   width: 1px;
      //   height: 100%;
      //   background-image: url(~@/assets/gradient-gray-line.png);
      //   background-size: contain;
      // }
    }
  }
}
</style>
